﻿using System;
using System.Collections;
using Model;
using NHibernate;
using NHibernate.Expression;

namespace DataAccess
{
    public class ReferenceDataAccess : BaseDataAccess
    {
        public int GetVGAId()
        {
            string query1 = "select e.EquipmentTypeId from EquipmentType as e where e.EquipmentTypeLabel like 'VGA'";
            IList result1 = GetObjects(query1);
            if (result1.Count != 0)
            return Int32.Parse(result1[0].ToString());
            return -1;

        }
        public int GetRamId()
        {
            string query1 = "select e.EquipmentTypeId from EquipmentType as e where e.EquipmentTypeLabel like 'ram'";
            IList result1 = GetObjects(query1);
            if (result1.Count != 0)
                return Int32.Parse(result1[0].ToString());
            return -1;

        }
        public int GetSoundCardId()
        {
            string query1 = "select e.EquipmentTypeId from EquipmentType as e where e.EquipmentTypeLabel like 'Sound Card'";
            IList result1 = GetObjects(query1);
            if (result1.Count != 0)
                return Int32.Parse(result1[0].ToString());
            return -1;

        }
        public int GetIntranetWORequestTypeId()
        {

            string query1 = "select e.RequestTypeId from RequestType as e where e.RequestTypeLabel like 'Intranet'";
            IList result1 = GetObjects(query1);
            if (result1.Count != 0)
                return Int32.Parse(result1[0].ToString());
            return -1;

        }
        public int GetCPUId()
        {
            string query1 = "select e.EquipmentTypeId from EquipmentType as e where e.EquipmentTypeLabel like 'cpu'";
            IList result1 = GetObjects(query1);
            if (result1.Count != 0)
                return Int32.Parse(result1[0].ToString());
            return -1;

        }
        public int GetNetworkCardId()
        {
            string query1 = "select e.EquipmentTypeId from EquipmentType as e where e.EquipmentTypeLabel like 'Network Card'";
            IList result1 = GetObjects(query1);
            if (result1.Count != 0)
                return Int32.Parse(result1[0].ToString());
            return -1;

        }
        public int GetHardiskId()
        {
            string query1 = "select e.EquipmentTypeId from EquipmentType as e where e.EquipmentTypeLabel like 'hardDrive'";
            IList result1 = GetObjects(query1);
            if (result1.Count != 0)
                return Int32.Parse(result1[0].ToString());
            return -1;

        }
        public EquipmentTypeSystem GetEquipmentTypeSystem(int typeId, int systemId)
        {
            try
            {

                EquipmentTypeSystem equipmentTypeSystem = null;
                ICriteria crit = m_session.CreateCriteria(typeof(EquipmentTypeSystem));
                crit.Add(Expression.Eq(EquipmentTypeSystem.CONST_TYPEID, typeId));
                crit.Add(Expression.Eq("SystemId", systemId));

                if (crit.List().Count != 0) equipmentTypeSystem = crit.List()[0] as EquipmentTypeSystem;
                return equipmentTypeSystem;


            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public EquipmentTypeModel GetEquipmentTypeModel(int typeId, int modelId)
        {
            try
            {
                EquipmentTypeModel equipmentTypeModel = null;
                ICriteria crit = m_session.CreateCriteria(typeof(EquipmentTypeModel));
                crit.Add(Expression.Eq(EquipmentTypeModel.CONST_TYPEID, typeId));
                crit.Add(Expression.Eq(EquipmentTypeModel.CONST_MODELID, modelId));
                
                if (crit.List().Count != 0) equipmentTypeModel = crit.List()[0] as EquipmentTypeModel;
                return equipmentTypeModel;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public IList GetRequestEmployees(int departmentId)
        {
            try
            {
                
                ICriteria crit = m_session.CreateCriteria(typeof(Employee));
                crit.Add(Expression.Eq(Employee.CONST_DEPARTMENTID, departmentId));
                crit.Add(Expression.Eq(Employee.CONST_REQUEST_WORKORDER, true));
                crit.AddOrder(Order.Asc(Employee.CONST_EMPLOYEE_NAME));
                return crit.List();

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public Employee GetEmployeeById(int employeeId)
        {
            try
            {

                ICriteria crit = m_session.CreateCriteria(typeof(Employee));
                crit.Add(Expression.Eq(Employee.CONST_EMPLOYEE_ID, employeeId));
                crit.AddOrder(Order.Asc(Employee.CONST_EMPLOYEE_NAME));
                return crit.List()[0] as Employee;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public Employee GetEmployeeByDomainName(string domainName)
        {
            try
            {

                Employee employee  = new Employee();
                ICriteria crit = m_session.CreateCriteria(typeof(Employee));
                crit.Add(Expression.Eq(Employee.CONST_DOMAIN_NAME, domainName));
                if (crit.List().Count != 0) employee = crit.List()[0] as Employee;
                return employee;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public Employee GetEmployeeByUserName(string userName)
        {
            try
            {
                Employee employee  = new Employee();
                ICriteria crit = m_session.CreateCriteria(typeof(Employee));
                crit.Add(Expression.Eq(Employee.CONST_USER_NAME, userName));
                if (crit.List().Count != 0) employee = crit.List()[0] as Employee;
                return employee;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
       
        public IList GetRequestEmployees()
        {
            try
            {

                ICriteria crit = m_session.CreateCriteria(typeof (Employee));
                crit.Add(Expression.Eq(Employee.CONST_REQUEST_WORKORDER, true));
                crit.AddOrder(Order.Asc(Employee.CONST_EMPLOYEE_NAME));
                return crit.List();

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList GetAssignedEmployees()
        {
            try
            {

                ICriteria crit = m_session.CreateCriteria(typeof(Employee));
                crit.Add(Expression.Eq(Employee.CONST0_ASSIGN_WORKORDER, true));
                crit.AddOrder(Order.Asc(Employee.CONST_EMPLOYEE_NAME));
                return crit.List();

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public Employee IsEmployeeExist(string userName, string password)
        {
            try
            {
                Employee emplyee = new Employee();
                ICriteria crit = m_session.CreateCriteria(typeof(Employee));
                crit.Add(Expression.Eq(Employee.CONST_USER_NAME, userName));
                crit.Add(Expression.Eq(Employee.CONST_PASSWORD, password));
                crit.AddOrder(Order.Asc(Employee.CONST_EMPLOYEE_NAME));
                if (crit.List().Count != 0) emplyee = crit.List()[0] as Employee;
                return emplyee;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }



    }
}
